package com.umeox.watch.moto.settings.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Switch;

import androidx.annotation.Nullable;

import com.umeox.moto.common.utils.AndroidUtils;
import com.umeox.moto.common.utils.DeviceManager;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.settings.R;

/**
 * Created by Rambo 2024/4/29/0029
 * Describe:
 */
public class CommonSwitchActivity extends ThemeAppCompatActivity {

    /**
     * 打开漫游广播
     */
    String INTENT_OPEN_DATA_ROAMING = "com.wherecom.intent.action.OPEN_DATA_ROAMING";

    /**
     * 关闭漫游广播
     */
    String INTENT_CLOSE_DATA_ROAMING = "com.wherecom.intent.action.CLOSE_DATA_ROAMING";

    /**
     * 控制voLTE开关广播
     * <p>
     * 额外数据的名称 voLTEStatus 0:关,1:开
     */
    private static final String ACTION_VOLTE_STATUS = "com.wherecom.intent.action.VOLTE_STATUS";
    private static final String EXTRA_VOLTE_STATUS = "voLTEStatus";

    private Switch mSwitch;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_common_switch);
        int type = getIntent().getIntExtra("type", 0);
        mSwitch = findViewById(R.id.s_data);
        initData(type);

        mSwitch.setOnCheckedChangeListener((view, checked) -> {
            switch (type) {
                case 0:
                    Intent intent = new Intent();
                    intent.setAction(checked ? INTENT_OPEN_DATA_ROAMING : INTENT_CLOSE_DATA_ROAMING);
                    sendBroadcast(intent);
                    break;
                case 1:
                    final Intent volteIntent = new Intent(ACTION_VOLTE_STATUS);
                    volteIntent.putExtra(EXTRA_VOLTE_STATUS, checked ? 1 : 0);
                    volteIntent.setPackage("com.android.settings");
                    sendBroadcast(volteIntent);
                    break;
                case 2:
                    DeviceManager.getInstance().setRaiseUpValue(checked);
                    break;
                case 3:
                    DeviceManager.getInstance().setOverlayScreenValue(checked);
                    break;
            }
        });

    }

    private void initData(int type) {
        switch (type) {
            case 0:
//                mTvTitle.setText(getString(R.string.data_roaming));
                int isDataRoaming = Settings.Global.getInt(this.getContentResolver(), Settings.Global.DATA_ROAMING, 0);
                mSwitch.setChecked(isDataRoaming == 1);
                break;
            case 1:
//                mTvTitle.setText(getString(R.string.volte));
                mSwitch.setChecked(AndroidUtils.isVoLTEEnabled(getApplicationContext()));
                break;
            case 2:
//                mTvTitle.setText(getString(R.string.str_hand_off));
                mSwitch.setChecked(DeviceManager.getInstance().isRaiseUpOn());
                break;
            case 3:
//                mTvTitle.setText(getString(R.string.str_overlay_screen));
                mSwitch.setChecked(DeviceManager.getInstance().isOverlayScreenOn());
                break;
        }
    }
}
